 /**************************************************************************//**
 * @file    StringHelper.c
 * @author  İsmail SEZEN (email: sezenismail@gmail.com)
 * @date    06.02.2011
 *
 * <b>Description</b>
 *
 * StringHelper.c file contains helper functions about string manipulatons.\n
 *
 * Copyright (C) 2010 İsmail SEZEN
 *
 * <b>License</b>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

/*Include Drectives*///{
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <array_helper.h>
//}

/*! @name Public
 *  These functions are defined in the header file StringHelper.h */
//@{//{

/*! @brief Counts the occurences of a char in the char array
  * @param[in] *str This is the char array being search.
  * @param[in] searchChar This is the char to be search.
  * @return Count of occurences */
int sh_count_char(const char* str, char searchChar) {
    register int count = 0;
    while (*str) if (*str++ == searchChar) count++;
    return count;
}

/*! @brief Parses the delimited text line into fields. Field must be in the buffer_size.
 *  @sa http://www.daniweb.com/code/snippet216535.html
 *  @param[in] *str is a pointer to the char array
 *  @param[in] buffer_size i maxs size of a field
 *  @param[in] delimiter is the delimiter char to split.
 *  @param[out] ***p is the pointer to 2D array splitted into fields. It must be NULL at the begining.
 *  @return Number of fields sperated by delimiter */
int sh_splitString2(const char* str, int buffer_size, char delimiter, char*** p) {
	if( strchr(str,delimiter) == NULL ) {
		int fieldCount = 1;
		char **data = arr_malloc(char*,fieldCount);
		data[0] = xmalloc(buffer_size);
		strcpy(data[0],str);
		*p = data;
		return fieldCount;
	}
	char searchString[10] = "%31[^;]%n\0";
	searchString[5] = delimiter;
	const char *ptr = str;
	int fieldCount = sh_count_char( str , delimiter ) + 1;
	char **data = arr_malloc(char*,fieldCount);
	int n,i = 0;
	while (*ptr) {
		data[i] = NULL;
		do{
			if(i>fieldCount) printf("Field count (%d) Overflow!",i);
			data[i] = xrealloc(data[i], buffer_size);
			int items_read = sscanf(ptr, searchString, data[i], &n);
			if(buffer_size>=n) {
				if (items_read == 1) ptr += n; /* advance the pointer by the number of characters read */
				if ( *ptr != delimiter) break; /* didn't find an expected delimiter, done? */
				++ptr; /* skip the delimiter */
				break;
			}else{
				buffer_size = n;
			}
		}while(1);
		i++;
	}
	*p = data;
	return fieldCount; // Success
}

/*! @brief Parses the delimited text line into fields. Field must be in the buffer_size.
 *  @sa http://www.daniweb.com/code/snippet216535.html
 *  @param *str is a pointer to the char array
 *  @param buffer_size i maxs size of a field.
 *  @param delimiter is the delimiter char to split.
 *  @return Returns a 2D array splitted into fields.
 *  @warning You must not forget to free the returning array */
char** sh_splitString(const char* str, int buffer_size, char delimiter) {
	char **data = NULL;
	sh_splitString2(str,buffer_size,delimiter,&data);
	return data;
}

//}//@}
